package com.yinsin.utils;

public class ByteUtils {
    /**
     * 将二进制转换成16进制
     * 
     * @param buf
     * @return
     */
    public static String parseByte2HexStr(byte buf[]) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < buf.length; i++) {
            String hex = Integer.toHexString(buf[i] & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            sb.append(hex.toUpperCase());
        }
        return sb.toString();
    }

    /**
     * 将16进制转换为二进制
     * 
     * @param hexStr
     * @return
     */
    public static byte[] parseHexStr2Byte(String hexStr) {
        if (hexStr.length() < 1)
            return null;
        byte[] result = new byte[hexStr.length() / 2];
        for (int i = 0; i < hexStr.length() / 2; i++) {
            int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
            int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);
            result[i] = (byte) (high * 16 + low);
        }
        return result;
    }

    /**
     * 过滤无效的byte
     * 
     * @param bytes
     * @return
     */
    public static byte[] filterByte(byte[] bytes) {
        byte[] byt = bytes;
        int len = bytes.length, index = 0;
        byte[] temp = new byte[len];
        if (bytes != null) {
            for (int i = 0; i < len; i++) {
                if (bytes[i] != 0) {
                    temp[index] = bytes[i];
                    index++;
                }
            }
            byt = new byte[index];
            for (int i = 0; i < index; i++) {
                byt[i] = temp[i];
            }
        }
        return byt;
    }

    /**
     * 复制byte[]
     * 
     * @param byt
     * @return new byte[]
     */
    public static byte[] copyByteArray(byte[] byt) {
    	byte[] nb = null;
        if (byt != null) {
            int len1 = byt.length;
            nb = new byte[len1];
            System.arraycopy(byt, 0, nb, 0, len1);
        }
        return nb;
    }

    /**
     * 合并两个byte数组，按入参顺序合并
     * 
     * @param byt1
     * @param byt2
     * @return new byte[]
     */
    public static byte[] joinByteArray(byte[] byt1, byte[] byt2) {
        int len1 = byt1.length, len2 = byt2.length;
        byte[] nb = new byte[len1 + len2];
        System.arraycopy(byt1, 0, nb, 0, len1);
        System.arraycopy(byt2, 0, nb, len1, len2);
        return nb;
    }

    /**
     * 将byte数组合并到另一个byte数组中（追加）
     * 
     * @param resByte
     * @param toByte
     * @return new byte[]
     */
    public static byte[] joinByteTo(byte[] resByte, byte[] toByte) {
        int len1 = resByte.length, len2 = toByte.length;
        byte[] nb = new byte[len1 + len2];
        System.arraycopy(toByte, 0, nb, 0, len2);
        System.arraycopy(resByte, 0, nb, len2, len1);
        return nb;
    }

    /**
     * 从大的byte数组中获取去前Len位数组
     * 
     * @param resByte
     * @param len
     * @return new byte[]
     */
    public static byte[] getByte(byte[] resByte, int len) {
        byte[] nb = new byte[len];
        System.arraycopy(resByte, 0, nb, 0, len);
        return nb;
    }

    /**
     * 从大的byte数组中从start索引处开始获取len长度的数组
     * 
     * @param resByte
     * @param start
     * @param len
     * @return new byte[]
     */
    public static byte[] getByte(byte[] resByte, int start, int len) {
        byte[] nb = new byte[len];
        System.arraycopy(resByte, start, nb, 0, len);
        return nb;
    }

}
